package com.shuoyue.adven.init;


import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.yf_rk3288_api.YF_RK3288_API_Manager;
import com.shuoyue.adven.CommonUtil;
import com.shuoyue.adven.MySpUtils;
import com.shuoyue.adven.R;
import com.shuoyue.adven.entity.AppVersionInfo;
import com.shuoyue.adven.entity.PowerTimePlan;
import com.shuoyue.adven.main.bussiness.MainFragmentActivity;
import com.shuoyue.adven.view.DownloadingDialog;
import com.sm.appbase.base.basemvp.BaseMvpActivity;
import com.sm.appbase.net.exception.NetErrorException;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class RegistActivity extends BaseMvpActivity<RegistContract.Presenter> implements RegistContract.View, VersionContract.View {


    @BindView(R.id.edit_code)
    EditText editCode;
    @BindView(R.id.edit_password)
    EditText editPassword;
    @BindView(R.id.check_version)
    Button checkVersion;
    VersionContract.Presenter versionPresenter;

    YF_RK3288_API_Manager yfapi;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_regist;
    }

    @Override
    protected void initView() {
        mPresenter = new RegistContract.Presenter();
        mPresenter.attachView(this);

        versionPresenter = new VersionContract.Presenter();
        versionPresenter.attachView(this);

        yfapi = new YF_RK3288_API_Manager(this);

    }

    @Override
    public void registResult(String token) {
        if (!TextUtils.isEmpty(token)) {
            MySpUtils.updateToken(mContext, token);
//            startActivity(new Intent(this, MainActivity.class));
            startActivity(new Intent(this, MainFragmentActivity.class));

            finish();
        }
    }


    @OnClick({R.id.submit, R.id.check_version})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.submit:
                String code = editCode.getText().toString();
                if (TextUtils.isEmpty(code)) {
                    Toast.makeText(this, "请输入激活码", Toast.LENGTH_SHORT).show();
                    return;
                }
                String password = editPassword.getText().toString();
                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(this, "请输入激活码", Toast.LENGTH_SHORT).show();
                    return;
                }
                mPresenter.regist(code, password);
                break;
            case R.id.check_version:
//                downloadStart();
                checkVersion.setEnabled(false);
                versionPresenter.getVersionInfo();
//                downloadSuc(new File("/storage/emulated/0/Android/data/com.shuoyue.adven/files/apk/mlb.apk"));
                break;
        }

    }

    @Override
    public void onError(NetErrorException throwable) {
        super.onError(throwable);
        Toast.makeText(mContext, throwable.getMessage(), Toast.LENGTH_LONG).show();
        checkVersion.setEnabled(true);
    }


    @Override
    public void downloadSuc(File f) {
        this.runOnUiThread(() -> {
            if (downloadingDialog != null && downloadingDialog.isShowing())
                downloadingDialog.dismiss();
            checkVersion.setEnabled(true);
//            NOpenFiles.openFile(this, f);
            yfapi.yfslientinstallapk(f.getAbsolutePath());
        });

    }

    @Override
    public void getVersion(AppVersionInfo appVersionInfo) {
        if (VersionContract.packageCode(mContext) < appVersionInfo.getVersion()) {
            //更新
            versionPresenter.downloadApk(appVersionInfo.getUrl(),
                    CommonUtil.getDownloadPath() + File.separator + "mlb.apk");
        } else {
            Toast.makeText(mContext, "已经是最新版本", Toast.LENGTH_SHORT).show();

        }
    }

    @Override
    public void progress(int progress) {
        runOnUiThread(() -> {
            if (downloadingDialog == null) {
                downloadStart();
            } else if (!downloadingDialog.isShowing()) {
                downloadingDialog.show();
            }
            downloadingDialog.setContent(progress + "%");
        });

    }

    DownloadingDialog downloadingDialog;

    @Override
    public void downloadStart() {
        runOnUiThread(() -> {
            if (downloadingDialog == null) {
                downloadingDialog = new DownloadingDialog(mContext, "下载新版本", "0%");
            }
            if (!downloadingDialog.isShowing()) {
                downloadingDialog.show();
            }
            downloadingDialog.setCancelable(false);
        });
    }

    @Override
    public void downloadError(String msg) {
        runOnUiThread(() -> {
            Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
            downloadingDialog.dismiss();
            checkVersion.setEnabled(true);
        });
    }

    @Override
    public void setPowerInfo(List<PowerTimePlan> planList) {

    }


}
